
class System {

  Particle_group particle_group;
  Particle[] particles;

  Collision_detector collision_detector;
  Gravity gravity;

  int number_of_particles;




  System(Particle_group particle_group) {
    this.particle_group = particle_group;

    particles = particle_group.particles;
    number_of_particles = particles.length;



    collision_detector = new Collision_detector();
   gravity = new Gravity();
  }

  void math() {

    //active particle outer for loop object being tested for :x
    //in active particles inner for loop objects being tested against :y

    for(int active=0; active<number_of_particles;active++) {
      for(int passive=0;passive<number_of_particles;passive++) {

        collision_detector.set_particle_one(particles[active]);
        gravity.set_particle_one(particles[active]);

        if((active !=passive) && (particles[passive].alive) && (particles[active].alive)) {         

          if(collision_detector.is_collision(particles[passive])) { 
            if(particles[active].mass >= particles[passive].mass) {

            // explode(particles[active],particles[passive]);
            //  merge(particles[active],particles[passive]);
              //collide(particles[active],particles[passive]);
            }
          }
          else {
            gravity.set_distance(collision_detector.distance);
            gravity.apply_gravity(particles[passive]);
           
          }
        }
      }
      //gravity.reset();
      //collision_detector.reset();
    }
  }


  void explode(Particle one,Particle two) {
    if( ( one.mass>two.mass) && (one.mass>=6)  ) {
      one.set_mass(one.mass-1);
      particle_group.live_particle(two.x_loc,two.y_loc);
      particle_group.kill_particle(two);
       
      for(int x=0; x < two.mass ; x++) {
        particle_group.live_particle(one.x_loc,one.y_loc);
      }
    }
  }

  void merge(Particle one,Particle two) {
    if(two.mass <6) {
      one.set_mass(one.mass+1);
      particle_group.kill_particle(two);
    }
  }
}

